/********************************************************************
	file base:	WindowsDllLoader
	file ext:	cpp
	author:		JiangHuai
	
	purpose:	windows dll loader
*********************************************************************/

#include "WindowsDllLoader.h"
#include <windows.h>
#include "predefine.h"
#include <utility>

#define DLL_PATH ".\\dll\\"

int WindowsDllLoader::LoadLibraryC(const string& name) {
	map<string, HINSTANCE>::const_iterator iter;
	iter = this->dllHandle.find(name);
	if (iter != dllHandle.end())
	{
		return 0;
	}

	HINSTANCE hDll = LoadLibrary(DLL_PATH + name);

	if (hDll == NULL)
	{
		return 1;
	}

	this->dllHandle.insert(map::value_type<string, HINSTANCE&>(name, hDll));

	return 0;
}

pair<funPointer, map<string, string> > WindowsDllLoader::FindProcC(const string& dllName, const string& name) {
//	HINSTANCE hDll = this->dllHandle[]
}

void WindowsDllLoader::FreeLibraryC(const string& dllName) {

	map<string, HINSTANCE>::const_iterator iter;
	iter = this->dllHandle.find(dllName);
	if (iter == dllHandle.end())
	{
		return;
	}
	else {
		FreeLibrary(iter->second);
		//this->dllHandle.erase(dllName);
	}
}

void WindowsDllLoader::FreeAllLibraries() {
	map<string, HINSTANCE>::iterator iter = map.begin();
	for( ; iter != map.end(); ++iter ) {
		this->FreeLibraryC(iter->first);
	}
}
/**/
